package com.noble.glamour.ui.user.my.auth;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.noble.glamour.R;
import com.noble.glamour.bean.my.IdCardVerifyStatusInfo;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.ChineseFilter;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.ui.user.my.view.AddPhotoView;
import com.noble.glamour.ui.utils.IDCardListener;
import com.noble.library.base.BaseActivity;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.utils.ScreenUtils;

/**
 * 身份证认证
 */
public class IDCardAuthenticationAct extends BaseActivity implements RequestComplete {

    private EditText eitName;
    private EditText eitIdCard;
    private AddPhotoView apvFrontPhoto;
    private AddPhotoView apvTailPhoto;
    private AddPhotoView apvHandPhoto;

    private String cardName, cardIdCard;
    private String strImgFront, strImgTail, strImgHand;

    private int authIDCard = 105;
    private IdCardVerifyStatusInfo mIdCardVerifyStatusInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.f1_id_card_auth_act);
        initView();
    }

    private void initView() {
        mIdCardVerifyStatusInfo = ModuleMgr.getCommonMgr().getIdCardVerifyStatusInfo();
        eitName = (EditText) findViewById(R.id.id_card_eit_name);
        eitIdCard = (EditText) findViewById(R.id.id_card_eit_id_card);
        apvFrontPhoto = (AddPhotoView) findViewById(R.id.id_card_apv_front_photo);
        apvTailPhoto = (AddPhotoView) findViewById(R.id.id_card_apv_tail_photo);
        apvHandPhoto = (AddPhotoView) findViewById(R.id.id_card_apv_hand_photo);
        setBg();

        initData();
        initTitle();
        eitIdCard.setKeyListener(new IDCardListener(eitIdCard));
    }

    private void initData() {
        eitName.setText(mIdCardVerifyStatusInfo.getAccountname());
        eitIdCard.setText(mIdCardVerifyStatusInfo.getId_num());
        if (!TextUtils.isEmpty(mIdCardVerifyStatusInfo.getId_front_img_small()) && !TextUtils.isEmpty(mIdCardVerifyStatusInfo.getId_back_img_small())
                && !TextUtils.isEmpty(mIdCardVerifyStatusInfo.getFace_img_small())) {
            apvFrontPhoto.setImg(mIdCardVerifyStatusInfo.getId_front_img_small());
            apvTailPhoto.setImg(mIdCardVerifyStatusInfo.getId_back_img_small());
            apvHandPhoto.setImg(mIdCardVerifyStatusInfo.getFace_img_small());
        }
    }

    private void initTitle() {
        setBackView();
        setTitle(getString(R.string.txt_authtype_id));
        if (mIdCardVerifyStatusInfo.getStatus() != 1 && mIdCardVerifyStatusInfo.getStatus() != 2) {
            setTitleRight(getString(R.string.title_right_submit), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cardName = eitName.getText().toString();
                    cardIdCard = eitIdCard.getText().toString();
                    if (TextUtils.isEmpty(cardName)) {
                        PToast.showShort(getString(R.string.name_cannot_be_empty));
                        return;
                    }
                    if (TextUtils.isEmpty(cardIdCard)) {
                        PToast.showShort(getString(R.string.idcard_cannot_be_empty));
                        return;
                    }
                    if (!ChineseFilter.idCardForm(cardIdCard)) {
                        PToast.showShort(getString(R.string.id_card_number_format_is_wrong));
                        return;
                    }

                    strImgFront = apvFrontPhoto.getImgStrPath();
                    strImgTail = apvTailPhoto.getImgStrPath();
                    strImgHand = apvHandPhoto.getImgStrPath();
                    if (TextUtils.isEmpty(strImgFront)) {
                        PToast.showShort(getString(R.string.please_upload_the_idcard_front));
                        return;
                    }
                    if (TextUtils.isEmpty(strImgTail)) {
                        PToast.showShort(getString(R.string.please_upload_the_idcard_tail));
                        return;
                    }
                    if (TextUtils.isEmpty(strImgHand)) {
                        PToast.showShort(getString(R.string.please_upload_the_idcard_handle));
                        return;
                    }

                    // TODO 认证音乐
//                    ModuleMgr.getCommonMgr().userVerify(cardIdCard, cardName, cardNum,
//                            paytype == 2 ? "" : cardLocal, paytype == 2 ? "" : cardLocalBranch,
//                            strImgFront, strImgTail, strImgHand, paytype, IDCardAuthenticationAct.this);
                }
            });
        }
    }

    private void setBg() {
        Drawable drawableFirst = getResources().getDrawable(R.drawable.f1_id_card_selector);
        drawableFirst.setBounds(0, 0, ScreenUtils.dp2px(this, 17), ScreenUtils.dp2px(this, 17));//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
        Drawable drawable = getResources().getDrawable(R.drawable.f1_id_card_selector);
        drawable.setBounds(0, 0, ScreenUtils.dp2px(this, 17), ScreenUtils.dp2px(this, 17));//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == authIDCard) {
            if (data != null) {
                int back = data.getIntExtra(IDCardAuthenticationSucceedAct.IDCARDBACK, 0);
                if (back == 1) {
                    data.putExtra(IDCardAuthenticationSucceedAct.IDCARDBACK, 1);
                    setResult(RESULT_OK, data);
                }
            }
        }
        this.finish();
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            ModuleMgr.getCenterMgr().getMyInfo().setIdcard_validation(1);
            MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null); //更新个人资料
            ModuleMgr.getCommonMgr().getVerifyStatus(null); //更新身份认证资料
            UIShow.showIDCardAuthenticationSucceedAct(this, authIDCard);
        }
        PToast.showShort(response.getMsg() + "");
    }
}
